# 创建数据库引擎
from typing import Annotated
from fastapi import Depends
from sqlalchemy.orm import sessionmaker
from sqlmodel import SQLModel, create_engine, Session

# echo=True会在控制台输出执行的SQL语句，方便调试
# check_same_thread=False是SQLite在多线程环境下的必要配置
engine = create_engine("mysql+pymysql://root:123456@127.0.0.1/test?charset=utf8mb4", echo=True)

# 创建会话，并把当前引擎绑定给该对话
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine, expire_on_commit=True)


def create_db_and_tables():
    SQLModel.metadata.create_all(engine)


# 创建会话本地化
def get_session():
    with Session(engine) as session:
        yield session


SessionDep = Annotated[Session, Depends(get_session)]


